<template>
  <div>
    <div class="app-container">
      <div class="search-container">
        <el-form :inline="true" :model="historyquerys" class="demo-form-inline">
          <!--转种日期:区间查询-->
          <el-form-item label="配种日期">
            <el-col :span="11">
              <el-date-picker v-model="historyquerys.sdate" type="date" placeholder="开始日期" style="width: 100%" />
            </el-col>
            <el-col :span="2" class="text-center">
              <span class="text-gray-500">-</span>
            </el-col>
            <el-col :span="11">
              <el-date-picker v-model="historyquerys.edate" placeholder="结束日期" style="width: 100%" />
            </el-col>
          </el-form-item>
          <el-form-item label="种母耳号">
            <el-input v-model="historyquerys.earTag" placeholder="请输入耳号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="GetHistoryList">查询</el-button>
            <el-button type="danger" :icon="Delete" @click="DelRange">批量删除</el-button>

            <el-button type="success" @click="AddHistory">新增</el-button>


          </el-form-item>
        </el-form>

        <el-table :data="tableData" style="width: 100%;height:100%;" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="hrid" label="序号" />
          <el-table-column prop="earTag" label="种母耳号" />
          <el-table-column prop="breedDate" label="配种日期">
            <template v-slot="scope">
              {{ scope.row.breedDate.substring(0, 10) }}
            </template>
          </el-table-column>
          <el-table-column prop="breedingRaw" label="种公耳号" />
          <el-table-column prop="pregnancyDate" label="妊检日期">
            <template v-slot="scope">
              {{ scope.row.pregnancyDate.substring(0, 10) }}
            </template>
          </el-table-column>
          <el-table-column prop="pregnancyResult" label="妊检结果" />
          <el-table-column prop="deliveryDate" label="分娩日期">
            <template v-slot="scope">
              {{ scope.row.deliveryDate.substring(0, 10) }}
            </template>
          </el-table-column>
          <el-table-column prop="healthyNumber" label="健羔数" />
          <el-table-column prop="healthyTotalWeight" label="健羔总重" />
          <el-table-column prop="lambNumber" label="弱羔数" />
          <el-table-column prop="malformationNumber" label="畸形数" />
          <el-table-column prop="stillbirthNumber" label="死胎数" />
          <el-table-column prop="mummyNumber" label="木乃伊数" />
          <el-table-column prop="freaksNumber" label="畸胎数" />

          <el-table-column prop="weaningDate" label="断奶日期">
            <template v-slot="scope">
              {{ scope.row.weaningDate.substring(0, 10) }}
            </template>
          </el-table-column>
          <el-table-column prop="weaningCount" label="断奶只数" />
          <el-table-column prop="weaningWeight" label="断奶窝重" />
          <el-table-column prop="currentPregnancy" label="当前胎次" />
          <el-table-column prop="createName" label="创建人" />
          <el-table-column prop="createTime" label="创建时间">
            <template v-slot="scope">
              {{ scope.row.createTime.substring(0, 10) }}
            </template>
          </el-table-column>
          <el-table-column prop="updateName" label="修改人" />
          <el-table-column prop="updateTime" label="修改时间">
            <template v-slot="scope">
              {{ scope.row.updateTime.substring(0, 10) }}
            </template>
          </el-table-column>

          <el-table-column prop="breedingBatchName" label="操作" fixed="right">
            <template v-slot="scope">
              <el-link :icon="Edit" type="primary" @click="edit(scope.row)">编辑</el-link>&emsp;
              <el-link :icon="Delete" type="danger" @click="del(scope.row.hrid)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="demo-pagination-block">
          <el-pagination v-model:current-page="historyquerys.pageIndex" v-model:page-size="historyquerys.pageSize"
            :page-sizes="[2, 3, 4, 5]" :background="true" layout="total, sizes, prev, pager, next, jumper"
            prev-text="上一页" next-text="下一页" :total="page.totalCount" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />

        </div>
        <el-dialog v-model="dialogVisible" title="修改种母历史记录" width="500">
          <span>
            <el-form :model="form" label-width="120px">

              <el-form-item label="种母耳号">
                <el-select v-model="form.earTag" placeholder="请选择耳号">
                  <el-option v-for="item in eweWeanings" :key="item.earTag" :label="item.earTag" :value="item.earTag" />
                </el-select>
              </el-form-item>
              <el-form-item label="配种日期">
                <el-date-picker v-model="form.breedDate" type="date" placeholder="选择配种日期" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" />
              </el-form-item>
              <el-form-item label="种公耳号">
                <el-input v-model="form.breedingRaw" />
              </el-form-item>
              <el-form-item label="妊检日期">
                <el-date-picker v-model="form.pregnancyDate" type="date" placeholder="选择妊检日期" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" />
              </el-form-item>
              <el-form-item label="妊检结果">
                <el-input v-model="form.pregnancyResult" />
              </el-form-item>
              <el-form-item label="分娩日期">
                <el-date-picker v-model="form.deliveryDate" type="date" placeholder="选择分娩日期" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" />
              </el-form-item>
              <el-form-item label="健羔数">
                <el-input v-model="form.healthyNumber" />
              </el-form-item>
              <el-form-item label="健羔总重">
                <el-input v-model="form.healthyTotalWeight" />
              </el-form-item>
              <el-form-item label="弱羔数">
                <el-input v-model="form.lambNumber" />
              </el-form-item>
              <el-form-item label="畸形数">
                <el-input v-model="form.malformationNumber" />
              </el-form-item>
              <el-form-item label="死胎数">
                <el-input v-model="form.stillbirthNumber" />
              </el-form-item>
              <el-form-item label="木乃伊数">
                <el-input v-model="form.mummyNumber" />
              </el-form-item>
              <el-form-item label="畸胎数">
                <el-input v-model="form.freaksNumber" />
              </el-form-item>
              <el-form-item label="当前胎次">
                <el-input v-model="form.currentPregnancy" />
              </el-form-item>
              <el-form-item label="断奶日期">
                <el-date-picker v-model="form.weaningDate" type="date" placeholder="选择分娩日期" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" />
              </el-form-item>
              <el-form-item label="断奶只数">
                <el-input v-model="form.weaningCount" />
              </el-form-item>
              <el-form-item label="断奶窝重">
                <el-input v-model="form.weaningWeight" />
              </el-form-item>
              <el-form-item>
                <el-button @click="UptHistory()" type="primary">确定</el-button>
                <el-button @click="dialogVisible = false" type="danger">取消</el-button>
              </el-form-item>
            </el-form>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
const router = useRouter();
//种母历史记录
import history from '@/api/HistoryRecord/index'
import { historyquery } from '@/api/HistoryRecord/model'
import { historyadd } from '@/api/HistoryRecord/model'
//耳号
import sheepAblactation from '@/api/ProductionManagement/SheepAblactation/index'
//对话框
const dialogVisible = ref(false)
import {

  Delete,
  Edit,
  Search,

} from '@element-plus/icons-vue'
//参数
const historyquerys = reactive<historyquery>({
  earTag: "",
  pageIndex: 1,
  pageSize: 15,
  sdate: "",
  edate: "",
})

//查询条件
const queryParams: any = reactive({
  ablactationAction: "",
  ablactationEnd: "",
  earTag: "",
  toBuildingManageId: "" || 0,
  userId: "" || 0,
  pageIndex: 1,
  pageSize: 15,
});
//分页
const page = reactive({
  totalCount: 0,
  pageCount: 0
})
//获取种母断奶列表
const eweWeanings = ref([])
const getAblactationList = () => {
  sheepAblactation.getSheepAblactationList(queryParams).then(res => {
    eweWeanings.value = res.data.data
    page.totalCount = res.data.totalCount
    page.pageCount = res.data.pageCount
  })
}


const handleSizeChange = (val: number) => {
  historyquerys.pageSize = val
  GetHistoryList()
}
const handleCurrentChange = (val: number) => {
  historyquerys.pageIndex = val;
  GetHistoryList()
}



//显示
const tableData: any = ref([])
const GetHistoryList = () => {
  history.getList(historyquerys).then((res: any) => {
    console.log('完整响应数据:', res);
    tableData.value = res.data.data
    page.pageCount = res.data.pageCount
    page.totalCount = res.data.totalCount
  })
}
onMounted(() => {
  GetHistoryList()
  getAblactationList()
})


//删除
const del = (id: number) => {
  ElMessageBox.confirm(
    '确认删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    history.deleteHirstory(id).then((res: any) => {
      ElMessage({
        message: res.msg,
        type: 'success',
      })
      GetHistoryList()
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除',
    })
  })

}




const idlist: any = ref([])
//已选择几项  
const checked = ref(0)
const handleSelectionChange = (val: any) => {
  checked.value = val.length; // 更新选中项的数量

  // 如果需要获取选中项的 ID 列表，可以进一步处理
  idlist.value = val.map((item: any) => item.hrid); // 假设每个选中项有一个唯一的 id 属性

}
//批量删除
const DelRange = () => {
  console.log(idlist.value)

  if (checked.value == 0) {
    ElMessage({
      message: '请选择要删除的项',
      type: 'warning',
    })
    return;
  }
  ElMessageBox.confirm(
    '确认批量删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    history.deleteBatchHirstory(idlist.value).then((res: any) => {
      ElMessage({
        message: res.msg,
        type: 'success',
      })
      GetHistoryList()
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除',
    })
  })


}

//跳转添加页面
const AddHistory = () => {
  router.push({
    name: "AddHistory"
  })
}

//修改页面
const form = reactive<historyadd>({
  hrid: 0,

  earTag: "",
  "breedDate": new Date(),
  "breedingRaw": "",
  "pregnancyDate": new Date(),
  "pregnancyResult": null,
  "deliveryDate": new Date(),
  "healthyNumber": null,
  "healthyTotalWeight": null,
  "lambNumber": null,
  "malformationNumber": null,
  "stillbirthNumber": null,
  "mummyNumber": null,
  "freaksNumber": null,
  "weaningDate": new Date(),
  "weaningCount": null,
  "weaningWeight": null,
  "currentPregnancy": null,
  createTime: "",


})

//修改
const edit = (data: any) => {
  dialogVisible.value = true;
  form.hrid = data.hrid
  getAblactationList()
  form.earTag = data.earTag
  form.breedDate = data.breedDate
  form.breedingRaw = data.breedingRaw
  form.pregnancyDate = data.pregnancyDate
  form.pregnancyResult = data.pregnancyResult
  form.deliveryDate = data.deliveryDate
  form.healthyNumber = data.healthyNumber
  form.healthyTotalWeight = data.healthyTotalWeight
  form.lambNumber = data.lambNumber
  form.malformationNumber = data.malformationNumber
  form.stillbirthNumber = data.stillbirthNumber
  form.mummyNumber = data.mummyNumber
  form.freaksNumber = data.freaksNumber
  form.weaningDate = data.weaningDate
  form.weaningCount = data.weaningCount
  form.weaningWeight = data.weaningWeight
  form.currentPregnancy = data.currentPregnancy
  form.createTime = data.createTime


}


const UptHistory = () => {
  ElMessageBox.confirm(
    '确定要修改吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    //修改
    history.updateHistory(form).then(res => {
      if (res.data > 0) {
        ElMessage({
          message: '修改成功',
          type: 'success',
        })
        dialogVisible.value = false;
        GetHistoryList()
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消修改',
    })
  })
}





</script>